// pages/package/package.js
const api = require('../../utils/api.js')
const util = require('../../utils/util.js')
const storage = require('../../utils/cache.js')
let num = 0
let click = true
let numValue = 0
let times = 5
Page({

  /**
   * 页面的初始数据
   */
  data: {
    num: 0,
    value: 0,
    animation: false,
    clear: '',
    endTime: 5,
    showcountDown: false
  },
  make(e) {
    if (util.isEmpty(e.detail)) {
      util.showFail('请点击元宝')
    } else {
      let data = {
        price: e.detail
      }
      storage.put('three', data)
      this.gift()
    }
  },
  onLoad(){
    click=true
    numValue=0
    times=5
  },
  gift(){
    wx.navigateTo({
      url: `../new/new`,
    })
  },
  animate() {
    if (click) {
      numValue++
      let nums = Math.ceil(Math.random() * 10)
      let values = this.data.value + nums
      this.setData({
        animation: true,
        num: nums,
        value: values,
        showcountDown: true
      })
      if (numValue == 1) {
        this.data.clear = setInterval(() => {
          times--
          if (times < 0) {
            times = 0
            clearInterval(this.data.clear)
            this.setData({
              showcountDown: false
            })
          } else {
            this.setData({
              endTime: times
            })
          }
        }, 1000)
        setTimeout(() => {
          this.setData({
            animation: false,
            num: 0
          })
          click = false
        }, 5000)
      }
      setTimeout(() => {
        this.setData({
          animation: false
        })
      },400)
    } else {
      console.log(click)
      return
    }
  },
  onShareAppMessage() {
    return {
      title: '一起来制作2019新年大礼包~',
      path: '/pages/home/home',
      imageUrl: '../../img/share.png',
    }
  },
})